#include "decoder_base.h"


DecoderBase::DecoderBase(AVStream* stream) {
    mQueue = new PacketQueue();
    mStream = stream;
}


DecoderBase::~DecoderBase() {
    if(mRunning)
    {
        stop();
    }
    free(mQueue);
    avcodec_close(mStream->codec);
}

void DecoderBase::enqueue(AVPacket *packet) {
    mQueue->put(packet);
}


int DecoderBase::packets() {
    return mQueue->size();
}

void DecoderBase::handleRun(void *ptr) {
    if (!prepare()) {
        LOGD("Couldn't prepare decoder");
        return;
    }
    decode(ptr);
}

bool DecoderBase::prepare()
{
    return false;
}

bool DecoderBase::process(AVPacket *packet)
{
    return false;
}

bool DecoderBase::decode(void *ptr) {
    return false;
}


//will be called from other thread
void DecoderBase::stop() {
    mQueue->abort();
    LOGD("waiting on end of decoder thread");
    int ret = -1;
    if((ret = join()) != 0) {
        LOGD("Couldn't cancel DecoderBase: %i", ret);
    }
}